Ensure binaries are present for tests
authorAlex Crichton <alex@alexcrichton.com>
Wed, 23 Jul 2014 18:58:09 +0000 (11:58 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 23 Jul 2014 19:00:18 +0000 (12:00 -0700)
Also make sure that binaries are named appropriately so they don't clash.

src/cargo/core/manifest.rs
src/cargo/util/toml.rs
tests/test_cargo_test.rs

index 301831fc084618268d295caf0b76c6a2d02c4e1a..fa752fa07c8cd19ad6588c32668a74bbb71aa5f9 100644 (file)
@@ -331,13 +331,14 @@ impl Target {
         }
     }
 
-    pub fn bin_target(name: &str, src_path: &Path, profile: &Profile) -> Target {
+    pub fn bin_target(name: &str, src_path: &Path, profile: &Profile,
+                      metadata: Option<Metadata>) -> Target {
         Target {
             kind: BinTarget,
             name: name.to_string(),
             src_path: src_path.clone(),
             profile: profile.clone(),
-            metadata: None
+            metadata: metadata,
         }
     }
 
@@ -347,7 +348,7 @@ impl Target {
             name: name.to_string(),
             src_path: src_path.clone(),
             profile: profile.clone(),
-            metadata: None
+            metadata: None,
         }
     }
 
index 1f9f1ffb01e6227d25daa00f90f72791c12ae8af..45dfacb1d236fd337ec79ddf8a5d9ebc4cee50cf 100644 (file)
@@ -532,22 +532,34 @@ fn normalize(libs: &[TomlLibTarget],
     }
 
     fn bin_targets(dst: &mut Vec<Target>, bins: &[TomlBinTarget],
-                   dep: TestDep, default: |&TomlBinTarget| -> String) {
+                   dep: TestDep, metadata: &Metadata,
+                   default: |&TomlBinTarget| -> String) {
         for bin in bins.iter() {
             let path = bin.path.clone().unwrap_or_else(|| {
                 TomlString(default(bin))
             });
 
             for profile in target_profiles(bin, dep).iter() {
+                let metadata = if profile.is_test() {
+                    // Make sure that the name of this test executable doesn't
+                    // conflicts with a library that has the same name and is
+                    // being tested
+                    let mut metadata = metadata.clone();
+                    metadata.mix(&format!("bin-{}", bin.name));
+                    Some(metadata)
+                } else {
+                    None
+                };
                 dst.push(Target::bin_target(bin.name.as_slice(),
                                             &path.to_path(),
-                                            profile));
+                                            profile,
+                                            metadata));
             }
         }
     }
 
     fn example_targets(dst: &mut Vec<Target>, examples: &[TomlExampleTarget],
-                   default: |&TomlExampleTarget| -> String) {
+                       default: |&TomlExampleTarget| -> String) {
         for ex in examples.iter() {
             let path = ex.path.clone().unwrap_or_else(|| TomlString(default(ex)));
 
@@ -566,11 +578,11 @@ fn normalize(libs: &[TomlLibTarget],
                 TomlString(default(test))
             });
 
-            let profile = &Profile::default_test();
             // make sure this metadata is different from any same-named libs.
             let mut metadata = metadata.clone();
-            metadata.mix(&format!("test-{}", test.name.as_slice()));
+            metadata.mix(&format!("test-{}", test.name));
 
+            let profile = &Profile::default_test();
             dst.push(Target::test_target(test.name.as_slice(),
                                          &path.to_path(),
                                          profile,
@@ -589,14 +601,14 @@ fn normalize(libs: &[TomlLibTarget],
     match (libs, bins) {
         ([_, ..], [_, ..]) => {
             lib_targets(&mut ret, libs, Needed, metadata);
-            bin_targets(&mut ret, bins, test_dep,
+            bin_targets(&mut ret, bins, test_dep, metadata,
                         |bin| format!("src/bin/{}.rs", bin.name));
         },
         ([_, ..], []) => {
             lib_targets(&mut ret, libs, test_dep, metadata);
         },
         ([], [_, ..]) => {
-            bin_targets(&mut ret, bins, test_dep,
+            bin_targets(&mut ret, bins, test_dep, metadata,
                         |bin| format!("src/{}.rs", bin.name));
         },
         ([], []) => ()
index 910f0437f147d70237f11262b42320d6b3d17ffb..1a0ff9c942cc889e4713919faf1a450250c63937 100644 (file)
@@ -38,8 +38,6 @@ test!(cargo_test_simple {
                                     test test_hello ... ok\n\n\
                                     test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n",
                                     COMPILING, p.root().display())));
-
-    assert_that(&p.bin("test/foo"), existing_file());
 })
 
 test!(many_similar_names {
@@ -109,8 +107,6 @@ test!(cargo_test_failing_test {
                                     0 ignored; 0 measured\n\n",
                                     COMPILING, p.root().display(),
                                     sep = path::SEP)));
-
-    assert_that(&p.bin("test/foo"), existing_file());
 })
 
 test!(test_with_lib_dep {
@@ -493,3 +489,30 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\
                        compiling = COMPILING,
                        dir = p.root().display()).as_slice()));
 })
+
+test!(bin_there_for_integration {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/main.rs", "
+            fn main() { std::os::set_exit_status(1); }
+            #[test] fn main_test() {}
+        ")
+        .file("tests/foo.rs", r#"
+            use std::io::Command;
+            #[test]
+            fn test_test() {
+                let status = Command::new("target/test/foo").status().unwrap();
+                assert!(status.matches_exit_status(1));
+            }
+        "#);
+
+    let output = p.cargo_process("cargo-test").exec_with_output().assert();
+    let output = str::from_utf8(output.output.as_slice()).assert();
+    assert!(output.contains("main_test ... ok"), "no main_test\n{}", output);
+    assert!(output.contains("test_test ... ok"), "no test_test\n{}", output);
+})